package com.lanchewuyou.user.driver.lipeiui;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lanchewuyou.user.driver.ui.CarPhotoActivity;
import com.lanchewuyou.user.driver.R;
import com.lanchewuyou.user.driver.modul.Config;
import com.lanchewuyou.user.driver.modul.OkHttpClientManager;
import com.squareup.okhttp.Request;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

public class CaseReturnActivity extends AppCompatActivity {
    private Toolbar mToolbar;
    private TextView caseReturncustomname, caseReturncustomphone, caseReturnownername, caseReturnownerphone, caseReturncasetype, caseReturnadd,
            caseReturnPhotoNumber;
    private LinearLayout caseReturnPhoto;
    private LinearLayout callCustom;
    public static String orderId;
    public Button caseReturnBtn;
    private ProgressDialog progressDialog;
    private static String DRIVERID;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_case_return);
        Intent intent = getIntent();
        String id = intent.getStringExtra("caseid");
        orderId = id;
        SharedPreferences sharedPreferences = getSharedPreferences(Config.PASSWORD_PATH, Activity.MODE_PRIVATE);
        DRIVERID = sharedPreferences.getString("driverid", "");
        initViews();
        progressDialog.show();
        OkHttpClientManager.getAsyn(Config.DRIVERORDERDETAIL + "&driverid=" + DRIVERID + "&orderid=" + orderId,
                new OkHttpClientManager.StringCallback() {
                    @Override
                    public void onFailure(Request request, IOException e) {
                        Toast.makeText(CaseReturnActivity.this, "加载失败", Toast.LENGTH_SHORT).show();
                        progressDialog.dismiss();
                    }

                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            String status = jsonObject.getString("status");
                            if (status.equals("1")) {
                                JSONObject info = jsonObject.getJSONObject("info");
                                caseReturncustomphone.setText(info.getString("ba_mobile"));
                                caseReturnownerphone.setText(info.getString("mobile"));
                                caseReturnownername.setText(info.getString("chezhu_name"));
                                caseReturncustomname.setText(info.getString("username"));
                                caseReturnadd.setText(info.getString("huanche_address"));
                                caseReturnPhotoNumber.setText(info.getJSONObject("carimg").getJSONObject("lists").getJSONObject("8").getInt("count") + "");
                                String accident = info.getString("accident");
                                if (accident.equals("1")) {
                                    caseReturncasetype.setText("双车事故（第三者）");
                                } else if (accident.equals("2")) {
                                    caseReturncasetype.setText("单车事故");
                                } else if (accident.equals("3")) {
                                    caseReturncasetype.setText("双车事故");
                                }
                                progressDialog.dismiss();
                            } else {
                                Toast.makeText(CaseReturnActivity.this, "没有获取到详情", Toast.LENGTH_SHORT).show();
                                progressDialog.dismiss();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                            progressDialog.dismiss();
                        }
                    }
                });
    }

    private void initViews() {
        mToolbar = (Toolbar) findViewById(R.id.case_return_toolbar);
        mToolbar.setTitle("还车详情");
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        caseReturncustomname = (TextView) findViewById(R.id.case_return_custom_name);
        caseReturncustomphone = (TextView) findViewById(R.id.case_return_custom_phone);
        caseReturnownername = (TextView) findViewById(R.id.case_return_owner_name);
        caseReturnownerphone = (TextView) findViewById(R.id.case_return_owner_phone);
        caseReturncasetype = (TextView) findViewById(R.id.case_return_case_type);
        caseReturnadd = (TextView) findViewById(R.id.case_return_add);
        caseReturnPhotoNumber = (TextView) findViewById(R.id.case_return_photo_num);
        callCustom = (LinearLayout) findViewById(R.id.case_return_call);
        callCustom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(Intent.ACTION_DIAL);
                i.setData(Uri.parse("tel:" + caseReturncustomphone.getText().toString()));
                startActivity(i);
            }
        });
        caseReturnPhoto = (LinearLayout) findViewById(R.id.case_return_photo);
        caseReturnPhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(CaseReturnActivity.this, CarPhotoActivity.class);
                intent.putExtra("from", "casereturn");
                startActivity(intent);
            }
        });
        caseReturnBtn = (Button) findViewById(R.id.case_return_next_btn);
        caseReturnBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        progressDialog = new ProgressDialog(CaseReturnActivity.this);
        progressDialog.setMessage("请稍等");
        progressDialog.setTitle("加载中");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setCancelable(true);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_case_return, menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
